package com.ruoyi.quartz.domain;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 
* @ClassName: ReturnJson
* @Description: rest请求返回值(JSON)
* @author pengjun
* @date 2019年1月10日
*
 */
public class ReturnJson {

    /**
     * JSON返回值数据体(data)
     */
    public static final String RETURN_JSON_DATA_KEY = "data";
    /**
     * JSON返回结果码
     */
    public static final String RETURN_JSON_RESULT_CODE = "code";
    /**
     * JSON返回结果信息
     */
    public static final String RETURN_JSON_RESULT_MESSAGE = "message";

    private final String code;
    private final String message;
    private final Object data;

    private ReturnJson(String resultCode, String resultMesg, Object data) {
        this.code = resultCode;
        this.message = resultMesg;
        this.data = data;
    }

    private ReturnJson(Object data) {
        this.code = ResponseCode.SUCCESS_CODE;
        this.message = ResponseCode.SUCCESS_MESG;
        this.data = data;
    }

    /**
     * 返回成功的JSON串
     *
     * @param data
     * @return
     */
    public static ReturnJson success(Object data) {
        return new ReturnJson(data);
    }

    /**
     * 返回成功的JSON串
     *
     * @return
     */
    public static ReturnJson success() {
        return ReturnJson.success(null);
    }

    /**
     *             {@link com.ruoyi.quartz.domain.common.base.framework.base.dds.model.other.core.model.entity.ReturnJson
     *             fail(String resultCode, Object data)}
     */
    public static ReturnJson fail(String resultCode, String resultMesg, Object data) {
        return new ReturnJson(resultCode, resultMesg, data);
    }

    /**
     *             {@link com.ruoyi.quartz.domain.common.base.framework.base.dds.model.other.core.model.entity.ReturnJson
     *             fail(String resultCode)}
     */
    public static ReturnJson fail(String resultCode, String resultMesg) {
        return new ReturnJson(resultCode, resultMesg, null);
    }
    /**
     * 
    * @Title: error
    * @Description: 系统异常
    * @param resultMesg
    * @return    参数
    * @return ReturnJson    返回类型
     */
    public static ReturnJson error(String resultMesg) {
    	return new ReturnJson(ResponseCode.SYSTEM_FAIL_CODE, resultMesg, null);
    }

    /**
     * 返回失败的JSON串
     *
     * @param resultCode
     * @param data
     * @return
     */
    public static ReturnJson fail(String resultCode, Object data) {
        return new ReturnJson(resultCode, ResponseCode.getMessage(resultCode), data);
    }

    public static ReturnJson fail(String resultCode) {
        return new ReturnJson(resultCode, ResponseCode.getMessage(resultCode), null);
    }

    /**
     * 根据结果码、结果信息、数据体生成实体
     *
     * @param resultCode
     * @param resultMesg
     * @param data
     * @return
     */
    public static ReturnJson json(String resultCode, String resultMesg, Object data) {
        return new ReturnJson(resultCode, resultMesg, data);
    }

    /**
     * 根据结果码、结果信息、数据体的Json String生成实体
     *
     * @return
     */
    public static ReturnJson json(String jsonString) {
        JSONObject object = JSONObject.parseObject(jsonString);
        String resultCode = (String) object.get(RETURN_JSON_RESULT_CODE);
        String resultMesg = (String) object.get(RETURN_JSON_RESULT_MESSAGE);
        Object data = object.get(RETURN_JSON_DATA_KEY);
        return new ReturnJson(resultCode, resultMesg, data);
    }

    /**
     * 返回系统异常的错误信息JSON串
     *
     * @return
     */
    public static ReturnJson systemError() {
        return ReturnJson.fail(ResponseCode.SYSTEM_FAIL_CODE, ResponseCode.SYSTEM_FAIL_MESG);
    }

    /**
     * 返回结果code
     *
     * @return
     */
    public String getCode() {
        return code;
    }

    /**
     * 返回结果具体信息
     *
     * @return
     */
    public String getMessage() {
        return message;
    }

    /**
     * 返回结果的具体数据
     *
     * @return
     */
    public Object getData() {
        return data;
    }

    /**
     * 根据成功与否标识,返回不同ReturnJson结果
     *
     * @param isSuccess
     *            成功与否
     * @param failCode
     *            处理异常时应该返回的code
     * @return
     */
    public static ReturnJson result(boolean isSuccess, String failCode) {
        if (isSuccess)
            return ReturnJson.success();

        return ReturnJson.fail(failCode);
    }

    /**
     * 根据结果码判断请求是否处理成功
     *
     * @return
     */
    @JsonIgnore
    public boolean isSuccess() {
        return ResponseCode.SUCCESS_CODE.equalsIgnoreCase(code);
    }

}
